package com.tty.intercepter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tty.entity.User;
import com.tty.mapper.UserMapper;
import com.tty.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName LoginIntercepter
 * @Description TODO
 * @Author Tommy Yeung
 * @Date 2022/11/21 上午 10:49
 **/


public class LoginIntercepter implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        String username = request.getParameter("username");
//        String password = request.getParameter("password");



        String uri = request.getRequestURI();


        //判断当前请求地址是否登录地址
        if (uri.contains("login")) {
            //登录请求，直接放行
            return true;
        } else {
//            Object userName = request.getSession().getAttribute("username");
//            System.out.println(userName);
//            if (request.getSession().getAttribute("username") != null) {
//                //说明已经登录，放行
//                return true;
//            } else {
                //没有登录，重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login");
//            }
            //默认拦截
            return false;

        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
